#ifndef POOL_H
#define POOL_H

#include "main.h"
#include "gameroom.h"
#include "player.h"

struct HdlHash {
    size_t operator()(const websocketpp::connection_hdl& wp) const {
        // 检查weak_ptr是否已经过期
        if (auto sp = wp.lock()) {
            // 如果weak_ptr没有过期，则使用shared_ptr的地址来哈希
            return hash<shared_ptr<void>>()(sp);
        } else {
            // 如果weak_ptr已经过期，则使用默认哈希值
            throw runtime_error("Connection Hdl has expired");
            return 0;
        }
    }
};

struct HdlEqual {
    bool operator()(const websocketpp::connection_hdl& ptr1, const websocketpp::connection_hdl& ptr2) const {
        if (auto shared1 = ptr1.lock()) {
            if (auto shared2 = ptr2.lock()) {
                return shared1 == shared2;
            }
        }
        // Handle expired weak_ptr, maybe return false or throw an exception.
        // This example returns false for expired pointers.
        return false;
    }
};

extern unordered_map<string, GameRoom*> roomPool;
extern unordered_map<string, Player*> playerPool;
extern unordered_map<websocketpp::connection_hdl, Player*, HdlHash, HdlEqual> hdlPool;

#endif